package com.xia.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xia.pojo.Category;
import com.xia.service.CategoryService;
import com.xia.utils.AjaxUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/type")
public class CategoryController {

    @Autowired
    public CategoryService categoryService;

    @RequestMapping("/queryList")
    @ResponseBody
    public String categoryQuery(){
        List<Category> categoryList = categoryService.listCategory();

        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("msg", "");
        result.put("count", categoryList.size());
        result.put("data", categoryList);

        try {
            return mapper.writeValueAsString(result);
        } catch (Exception e) {
            e.printStackTrace();
            return "{\"code\":-1,\"msg\":\"JSON处理错误\"}";
        }
    }

    @PostMapping("/edit")
    @ResponseBody
    public String editCategory (Category category){
        AjaxUtils ajaxUtils = new AjaxUtils();
        try {
            categoryService.updateCategory(category);
            ajaxUtils.setSuccess(true);
            ajaxUtils.setCode(0);
            ajaxUtils.setStatus("0");
            ajaxUtils.setMessage("更新成功！");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxUtils.setSuccess(false);
            ajaxUtils.setCode(-1);
            ajaxUtils.setStatus("-1");
            ajaxUtils.setMessage("更新失败！");
        }

        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(ajaxUtils);
        } catch (Exception e) {
            e.printStackTrace();
            return "{\"code\":-1,\"msg\":\"JSON处理错误\"}";
        }
    }

    @PostMapping("/delType")
    @ResponseBody
    public String delCategory (Integer cid){
        AjaxUtils ajaxUtils = new AjaxUtils();
        try {
            categoryService.delCategory(cid);
            ajaxUtils.setSuccess(true);
            ajaxUtils.setMessage("删除成功！");
            ajaxUtils.setCode(0);
            ajaxUtils.setStatus("0");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxUtils.setSuccess(false);
            ajaxUtils.setMessage("删除失败！");
            ajaxUtils.setCode(-1);
            ajaxUtils.setStatus("-1");
        }

        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(ajaxUtils);
        } catch (Exception e) {
            e.printStackTrace();
            return "{\"code\":-1,\"msg\":\"JSON处理错误\"}";
        }
    }

    @PostMapping("/addType")
    @ResponseBody
    public String addType (Category category){
        AjaxUtils ajaxUtils = new AjaxUtils();
        try {
            categoryService.addType(category);
            ajaxUtils.setSuccess(true);
            ajaxUtils.setMessage("新增成功！");
            ajaxUtils.setCode(0);
            ajaxUtils.setStatus("0");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxUtils.setSuccess(false);
            ajaxUtils.setMessage("新增失败！");
            ajaxUtils.setCode(-1);
            ajaxUtils.setStatus("-1");
        }

        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(ajaxUtils);
        } catch (Exception e) {
            e.printStackTrace();
            return "{\"code\":-1,\"msg\":\"JSON处理错误\"}";
        }
    }
}